package com.luyang.framework.validator;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.lang.reflect.InvocationTargetException;

/**
 * 异常处理工具类
 *
 * @author lu_yang
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class Exceptions {

    /**
     * 将 CheckedException 转换为 UncheckedException
     *
     * @param e Throwable
     * @return java.lang.RuntimeException
     * @author lu_yang
     */
    public static RuntimeException unchecked(Throwable e) {
        if (e instanceof IllegalAccessException || e instanceof IllegalArgumentException
            || e instanceof NoSuchMethodException) {
            return new IllegalArgumentException(e);
        } else if (e instanceof InvocationTargetException invocation) {
            return new RuntimeException((invocation.getTargetException()));
        } else if (e instanceof RuntimeException runtime) {
            return runtime;
        } else {
            return new RuntimeException(e);
        }
    }
}
